<!DOCTYPE html>
<html>
<head>
        <title>攻略系列</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategyCatalogs.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/common.js"></script>
    <script src="/js/checkLogin.js"></script>
  <!--  <script src="/js/plugins/dist/mDialogMin.js"></script>
    <link rel="stylesheet" href="/js/plugins/dist/dialog.css"/>
-->
    <script>
        $(function(){
            //获取攻略id
            var strategyId = getParams().id;
            console.log(strategyId);
            if (strategyId) {
                /**
                 * 根据攻略id获取对应的攻略对象
                 * 资源:/strategies/{strategyId}
                 * 请求方式:get
                 * 请求参数:
                 * 返回值:Strategy
                 */
                $.get(baseUrl+"/strategies/"+strategyId,function(data){
                    $(".strategies").renderValues(data);
                });

                /**
                 * 根据攻略id获取对应的类别信息
                 * 资源:/strategies/{strategyId}/catalogs
                 * 请求方式:get
                 * 请求参数:
                 * 返回值:List
                 */
                $.get(baseUrl+"/strategies/"+strategyId+"/catalogs",function(data){
                    var index = 1;
                    console.log(data);
                    $(".catalogs").renderValues({list:data},{
                        getName:function(item,value){
                            $(item).html(index++ +"/"+value);
                        },
                        getHref:getHref
                    });
                });
                /**
                 * 根据攻略id获取对应的点评信息
                 * 资源:/strategies/{strategyId}/comments
                 * 请求方式:get
                 * 请求参数:
                 * 返回值:PageInfo
                 */
                /*$.get(baseUrl+"/strategies/"+strategyId+"/comments",function(data){
                    $("#comment").renderValues(data,{
                        getStarNum:function(item,value){
                            var star = '';
                            for (var i =0;i<value;i++){
                                star+='<i class="fa fa-star"></i>';
                            }
                            for (var i =0;i<5-value;i++){
                                star+='<i class="fa fa-star-o"></i>';
                            }
                            $(item).html(star);
                        },
                        getHref:getHref
                    });
                });*/
                var currentPage = 1;//定义当前页,默认为1
                var arr = [];//定义一个数组,用来装所有的分页数据
                var totalPage;//定义总页数
                var loading = false;//判断是否真正加载数据
                function query() {
                    if (loading == true) {
                        return;
                    }
                    loading = true;
                    $.get(baseUrl + "/strategies/"+strategyId+"/comments", {currentPage: currentPage}, function (data) {
                        //console.log(data);
                        totalPage = data.pages;
                        //合并数据
                        $.merge(arr, data.list);
                        //改变数据格式
                        var json = {list: arr};
                        // 使用jrender 插件渲染数据 到页面
                        $("#comment").renderValues(json, {
                            getStarNum:function(item,value){
                                var star = '';
                                for (var i =0;i<value;i++){
                                    star+='<i class="fa fa-star"></i>';
                                }
                                for (var i =0;i<5-value;i++){
                                    star+='<i class="fa fa-star-o"></i>';
                                }
                                $(item).html(star);
                            },
                            getHref: getHref
                        });
                        currentPage++;
                        loading = false;
                        $(".comment-img img").click(function(){
                            var path = $(this).attr("src");
                            $(document).dialog({
                                overlayClose: true,
                                style:'ios',
                                content: '<img src="'+path+'" width="300px">',
                            });
                        });
                    });
                };
                query();
                $(window).scroll(function () {
                    if ($(window).height() + $(document).scrollTop()+1 >= $(document).height()) {
                        if (currentPage <= totalPage) {
                            query();
                        } else {
                            $(document).dialog({
                                type: 'notice',
                                infoText: '已经到底啦',
                                autoClose: 2500,
                                position: 'bottom'  // center: 居中; bottom: 底部
                            });
                        }
                    }
                });
                $("#commentBtn").click(function(){
                    location.href="/mine/addComment.html?id="+strategyId;
                });

                /**
                 * 获取评价的标签
                 * 资源:/strategies/{strategyId}/tags
                 * 请求方式:get
                 * 请求参数:
                 * 返回值:List<Tag>
                 */
                $.get(baseUrl + "/strategies/"+strategyId+"/tags",function(data){
                    $(".tag").html();
                    var tags = '';
                    for(var i=0;i<data.length;i++){
                        tags+='<span>'+data[i].name+'</span>';
                    }
                    $(".tag").html(tags);
                });
            };

            //收藏操作=======================================================================
           //查询收藏该攻略的数量
            $.get(baseUrl+"/likesAndCollects"+"/collectNum", {strategyId: strategyId}, function (data) {
                $('#strategyCount').html(data);
            })
            //获得当前用户
            var user = JSON.parse(sessionStorage.getItem("user"));
            //console.log(user);
            $('.collectNum').click(function () {
                //判断是收藏还是取消收藏
                    if ($(".collectNum").data('status') == 0) {
                        num = $("#strategyCount").html();
                        num++;
                        $("#strategyCount").html(num);
                        $(".collectNum").removeClass("fa-heart-o").addClass("fa-heart")
                        $(".collectNum").data('status', 1);
                        $.ajax({
                            type: "post",
                            url: baseUrl+"/likesAndCollects"+"/collect",
                            data: {userId: user.id,strategyId: strategyId, status: 1},
                            success: function (data) {
                                //console.log(data);
                                //$("#collect").renderValues(data);
                                if (data.success) {
                                    $(document).dialog({
                                        overlayClose: true,
                                        content: '收藏成功'
                                    });
                                }
                            }
                        })

                    } else if ($(".collectNum").data('status') == 1) {
                        num = $("#strategyCount").html();
                        num--;
                        $("#strategyCount").html(num);
                        $(".collectNum").data('status', 0);
                        $(".collectNum").removeClass("fa-heart").addClass("fa-heart-o")
                        console.log(strategyId);
                        $.ajax({
                            url: baseUrl+"/likesAndCollects"+"/deleteCollect/"+user.id+"/"+strategyId,
                            type: 'DELETE',
                            dataType: 'json',
                            success: function (data) {
                                //$(".focus").renderValues(data);
                                $(document).dialog({
                                    overlayClose: true,
                                    content: '取消收藏成功'
                                });
                            }
                        })
                    }
            })
            //收藏回显操作
            if (user) {
                $.get(baseUrl+"/likesAndCollects"+"/collectStatus",{userId:user.id,strategyId:strategyId},function (data) {
                    $('.collectNum').data("status",data);
                    if(data==1){
                        $(".collectNum").removeClass("fa-heart-o").addClass("fa-heart")
                    }else{
                        $(".collectNum").removeClass("fa-heart").addClass("fa-heart-o")
                    }
                })
            }


            //点赞操作=======================================================================
            //查询点赞该攻略的数量
            $.get(baseUrl+"/likesAndCollects"+"/likeNum", {strategyId: strategyId}, function (data) {
                $('#strategyLike').html(data);
            })
            $('.likeNum').click(function () {
                //判断是点赞还是取消点赞
                if ($(".likeNum").data('status') == 0) {
                    num = $("#strategyLike").html();
                    num++;
                    $("#strategyLike").html(num);
                    $(".likeNum").removeClass("fa-thumbs-o-up").addClass(" fa-thumbs-up")
                    $(".likeNum").data('status', 1);
                    $.ajax({
                        type: "post",
                        url: baseUrl+"/likesAndCollects"+"/like",
                        data: {userId: user.id,strategyId: strategyId, status: 1},
                        success: function (data) {
                            if (data.success) {
                                $(document).dialog({
                                    overlayClose: true,
                                    content: '点赞成功'
                                });
                            }
                        }
                    })

                } else if ($(".likeNum").data('status') == 1) {
                    num = $("#strategyLike").html();
                    num--;
                    $("#strategyLike").html(num);
                    $(".likeNum").removeClass("fa-thumbs-up").addClass("fa-thumbs-o-up")
                    $(".likeNum").data('status', 0);
                    $.ajax({
                        url: baseUrl+"/likesAndCollects"+"/deleteLike/"+user.id+"/"+strategyId,
                        type: 'DELETE',
                        dataType: 'json',
                        success: function (data) {
                            $(document).dialog({
                                overlayClose: true,
                                content: '取消点赞成功'
                            });
                        }
                    })
                }
            })
            //点赞回显操作
            if (user) {
                $.get(baseUrl+"/likesAndCollects"+"/likeStatus",{userId:user.id,strategyId:strategyId},function (data) {
                    $('.likeNum').data("status",data);
                    if(data==1){
                        $(".likeNum").removeClass("fa-thumbs-o-up").addClass("fa-thumbs-up")
                    }else{
                        $(".likeNum").removeClass("fa-thumbs-up").addClass("fa-thumbs-o-up")
                    }
                })
            }

            if (strategyId) {
                //0:没有加入旅行单;1:加入旅行单
                var travelState;//判断是否关注状态
                //判断登录用户是否加入了旅行单
                $.get(baseUrl+"/strategies/"+user.id+"/"+strategyId,function(data){
                    travelState=data;
                    console.log(travelState);
                    if (travelState == 1) {
                        $("#travelAlone").html("取消旅行单")
                    }else if (travelState == 0){
                        $("#travelAlone").html("加入旅行单")
                    }
                });
                //加入旅行单(按钮添加)
                $("#travelAlone").click(function(){
                    if (travelState == 1) {
                        travelState = 0;
                        $(this).html("加入旅行单");
                        //取消旅行单
                        $.ajax({
                            url:baseUrl+"/strategies/unTravelAlone/"+user.id+"/"+strategyId,
                            type:"delete"
                        });
//                        $.messager.alert("温馨提示:","取消旅行单成功");
                    }else if (travelState == 0){
                        travelState = 1;
                        $(this).html("取消旅行单");
                        //添加旅行单
                        $.ajax({
                            url:baseUrl+"/strategies/travelAlone/"+user.id+"/"+strategyId,
                            type:"post"
                        });
//                        $.messager.alert("温馨提示:","加入旅行单成功");
                    }
                });
            }

            //搜索框
            //定义全局变量获取搜索框内容
            var search;
            $("#searchBtn").val('');
            //给搜索框绑定点击事件
            $("#searchBtn").click(function () {
                //获取搜索框的内容
                search=$(this).val();
            });
            //给搜索按钮绑定点击事件
            $("#buttonSearch").click(function () {
                //调用点击事件
                $("#searchBtn").click();
                console.log(search);
                if (search!=null && search){
                    $.ajax({
                        url:baseUrl+"/strategies/{search}",
                        type:'post',
                        data:{search:search},
                        success:function (data) {
                            console.log(data);
                            //将除搜索外的所有标签全部隐藏起来
                            $(".strategies").attr("hidden",true);
                            $("#collectLike").attr("hidden",true);
                            $("#pills-tab").attr("hidden",true);
                            $("#pills-tabContent").attr("hidden",true);
                            $(".operation").attr("hidden",true);
                            //将隐藏的div解除隐藏
                            $(".strategyHidden").attr("hidden",false);
                            //对div进行数据渲染
                            $(".strategyHidden").renderValues(data,{
                                getStrategyId:getHref
                            });
                            $(".search-head a").attr("href","");
                        }
                    });
                }
            })


        })
    </script>
</head>
<style>
    body {
        background: url("/upload/2.bmp") no-repeat center center fixed;
        -webkit-background-size: cover;
        -moz-background-size: cover;
        -o-background-size: cover;
        background-size: cover;
    }
</style>

<body>
<div class="search-head" style="background: url('/upload/2.bmp')">
    <div class="row nav-search">
        <div class="col-1">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="form-control " id="searchBtn" placeholder="搜索旅行攻略">
            </div>
            <a href="#">
                <i class="fa fa-search" id="buttonSearch"
                   style="color:white;font-size:25px;position: absolute;top: 23%;left: 92%">
                </i>
            </a>
        </div>
        <div class="col-1">
            <!--<button class="btn" id="buttonSearch">搜索</button>-->
        </div>
    </div>
</div>
<!--定义隐藏div,用于搜索显示-->
<div class="container strategyHidden" hidden>
    <div class="row hot" render-loop="list">
        <div class="col-12">
            <a data-url="strategyCatalogs.html?id=" render-fun="getStrategyId" render-key="list.id">
                <img render-src="list.coverUrl">
                <p render-html="list.title">成都攻略</p>
            </a>
        </div>
    </div>
</div>
<hr>
<div class="container strategies">
    <h5 class="title" render-html="title">广州攻略</h5>
    <!--<small><span>0</span>人阅读过此攻略</small>-->
    <div></div>
</div>
<div id="collectLike">
    <div class="focus">
        <i class="fa fa-heart-o collectNum" data-status="0">
            <span id="strategyCount">0</span> 收藏
        </i>
        <i class="fa fa-thumbs-o-up likeNum" data-status="0">
            <span id="strategyLike"> 0</span> 点赞
        </i>
       <!-- <button class="btn">想去</button>
        <button class="btn">去过</button>-->
    </div>
</div>
<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab">
    <li class="nav-item ">
        <a class="nav-link active" id="pills-catalog-tab" data-toggle="pill" href="#pills-catalog">
            <span>攻略目录</span>
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link " id="pills-comment-tab" data-toggle="pill" href="#pills-comment">
            <span>骡友点评</span>
        </a>
    </li>
</ul>
<div class="tab-content " id="pills-tabContent">
    <div class="tab-pane fade show active" id="pills-catalog">

        <div class="catalogs">
            <div render-loop="list">
                <div class="container detail">
                    <h6 render-fun="getName" render-key="list.name">01/概况</h6>
                    <div render-loop="list.details">
                        <a data-url="strategyDetails.html?id=" render-fun="getHref" render-key="list.details.id">
                            <button class="btn" render-html="list.details.title">广州速览</button>
                        </a>
                    </div>
                </div>
            </div>
        </div>

    </div>
    <div class="tab-pane fade " id="pills-comment">

        <div class="container tag">
            <h6>骡友标签</h6>
            <div>
                <span>价格便宜</span>
                <span>很热闹</span>
                <span>交通方便</span>
                <span>吃的很多</span>
                <span>风景优美</span>
                <span>美食很多</span>
            </div>
        </div>
        <hr>

        <div id="comment">
            <div render-loop="list">
                <div class="container row comment">
                    <div class="col-2 comment-head">
                        <a data-url="userProfiles.html?id=" render-fun="getHref" render-key="list.user.id">
                            <img class="rounded-circle" render-src="list.user.headImgUrl">
                        </a>
                    </div>
                    <div class="col comment-right">
                        <p class="authorName"><span render-html="list.user.nickName">喵喵</span></p>
                        <span class="comment-star" render-fun="getStarNum" render-key="list.starNum">
                            <!--<i class="fa fa-star"></i>
                            <i class="fa fa-star"></i>
                            <i class="fa fa-star"></i>
                            <i class="fa fa-star"></i>
                            <i class="fa fa-star-o"></i>-->
                        </span>
                        <span class="comment-date" render-html="list.createTime">2018-07-19</span>
                        <div class="comment-content">
                            <a data-url="strategyComment.html?id=" render-fun="getHref" render-key="list.id">
                                <p render-html="list.content">我觉得很好</p>

                            </a>
                        </div>
                        <ul class="comment-img" render-loop="list.imgsUrls">
                            <li>
                                <img  render-src="list.imgsUrls.self">
                            </li>
                        </ul>
                      <!--  <div class="d-flex icon">
                            <a href="strategyComment.html">
                                  <i class="fa  fa-commenting-o " style="margin-right: 10px"> 评论 </i>
                                <i class="fa fa-thumbs-o-up"> <span></span> </i>
                            </a>
                        </div>-->
                </div>

                </div>
            </div>
        </div>

    </div>
</div>

<div class="operation">
    <button class="btn" id="travelAlone">加入旅行单</button><button class="btn" id="commentBtn">点评</button>
</div>


</body>
</html>